/** Text document reader mode, used for help files and story text.
@param buffer Screen buffer to display to.
@param text Text to display.
@param callback Callback method to be called when the text reader is closed.
@param context Context of the callback method.
*/
function TextReaderMode(buffer, text, callback, context)
{
	this.buffer = buffer;
	this.lines = text.split("\n");
	this.height = this.lines.length;
	if(this.height > this.buffer.height - 8)
		this.height = this.buffer.height - 8;
	this.width = 50;
	this.callback = callback;
	this.callbackContext = context;
	this.top = 0;
	this.render();
}
TextReaderMode.prototype =
{
	/** Handle an input action.
	 *@param action The action string to handle.
	*/
	handleAction: function(action)
	{
		switch(action)
		{
			case "up": --this.top; break;
			case "down": ++this.top; break;
			case "left": this.top -= this.height - 2; break;
			case "right": this.top += this.height - 2; break;
			case "use":
			case "cancel":
				removeMode(this);
				if(this.callback)
					this.callback.call(this.callbackContext);
				return true;
				break;
			default: return true; break;
		}
		if(this.top < 0)
			this.top = 0;
		if(this.top + this.height > this.lines.length)
			this.top = this.lines.length - this.height;
		this.render();
		return true;
	},
	/** Rerender the text.
	*/
	render: function()
	{
		var iLine = this.top;
		var width = this.width;
		if(this.height < this.lines.length)
			width += 1;
		var left = Math.floor((this.buffer.width - width) / 2);
		var top = Math.floor((this.buffer.height - this.height) / 2);
		this.buffer.drawMenuBox(left, top, width, this.height);
		if(this.height < this.lines.length)
		{
			this.buffer.blitRect(left + this.width, top, 1, this.height, " ", null, Color.darkGray);
			var pos = Math.floor((this.top / (this.lines.length - this.height)) * this.height);
			pos = Math.min(pos, this.height - 1);
			this.buffer.setCell(left + this.width, top + pos, " ", null, Color.gray);
		}
		for(var i = 0; i < this.height; ++i)
		{
			if(iLine < this.lines.length)
			{
				this.buffer.blitString(left, top + i, this.lines[iLine], this.width);
				++iLine;
			}
			else
				this.buffer.blitString(left, top + i, "", this.width);
		}
	}
}
